package com.example.demo.controller;

import com.example.demo.entity.Book;
import com.example.demo.entity.Category;
import com.example.demo.service.BookService;
import com.example.demo.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/book")
public class BookController {

    @Autowired
    BookService bookService;

    @Autowired
    CategoryService categoryService;

    @RequestMapping("/list")
    public String list(Model model, @RequestParam(value = "page", defaultValue = "0") Integer page,
                       @RequestParam(value = "size", defaultValue = "5") Integer size) {
        if (page == null || page <= 0) {
            page = 1;
        } // 将第0页转换为习惯的第1页
        if (size == null || size <= 0) {
            size = 5;
        }
        Sort sort = Sort.by(Sort.Direction.ASC, "id"); // 排序字段是实体类的属性
        Pageable pageable = PageRequest.of(page - 1, size, sort); // （当前页，每页记录数，排序方式）
        Page<Book> bookPage = bookService.getBookList(pageable);
        model.addAttribute("bookPage", bookPage);
        model.addAttribute("size", size);


        return "b/list";
    }


//    @RequestMapping("/list")
//    public String list(Model model) {
//        List<Book> bookList = bookService.searchAll();
//        model.addAttribute("books", bookList);
//        return "b/list";
//    }

    @RequestMapping("/init")
    public String init(Model model) {
        bookService.init();
        List<Book> bookList = bookService.searchAll();
        model.addAttribute("books", bookList);
        return "b/list";
    }


//    @RequestMapping("/all")
//    public List<Book> search(){
//        return bookService.searchAll();
//    }
//
//    @GetMapping("/get/{id}")
//    public Book get(@PathVariable Long id){
//        Book book=bookService.getBookById(id);
//        return book;
//    }
//
//    @GetMapping("/search/{name}")
//    public List<Book> search(@PathVariable String name){
//        return bookService.searchByName(name);
//    }


    @RequestMapping("/find")
    public String ddd(Model model, @RequestParam(value = "bookname") String name) {
        List<Book> bookList = bookService.searchByName(name);
        model.addAttribute("list", bookList);
        return "b/show";
    }

    @RequestMapping("/findpublish")
    public String dd(Model model, @RequestParam(value = "pubname") String name) {
        List<Book> bookList = bookService.searchByPub(name);
        model.addAttribute("list", bookList);
        return "b/show";
    }

    @RequestMapping("/findcategory")
    public String dda(Model model, @RequestParam(value = "category") String cat) {
        List<Book> bookList = bookService.searchByCat(cat);
        model.addAttribute("list", bookList);
        return "b/show";
    }


    @RequestMapping("/edit/{id}")
    public String edit(Model model, @PathVariable Long id) {
        Book book = bookService.getBookById(id);
        model.addAttribute("booke", book);
        return "b/edit";//打开创建图书修改界面
    }

    @PostMapping("/edit2")
    public String modify(Book book, @RequestParam("cat_id") Long c_id) {

        Book s = bookService.getBookById(book.getId());
        s.setIsbn(book.getIsbn());
        s.setName(book.getName());
        s.setAuthor(book.getAuthor());
        s.setPublish(book.getPublish());
        s.setPrice(book.getPrice());
        Category category = categoryService.getCategoryById(c_id);
        s.setCategory(category);
        bookService.modify(s);
        return "redirect:/book/list";
    }

    @RequestMapping("/delete/{id}")
    public String dsd(Model model, @PathVariable Long id) {
        Book book = bookService.getBookById(id);
        model.addAttribute("booke1", book);
        return "b/delete";//打开创建图书删除界面
    }

    @PostMapping("/delete2")
    public String delete(@RequestParam(value = "id") Long id) { //确认删除
        String name1 = bookService.getBookById(id).getName();
        bookService.delete(name1);
        return "redirect:/book/list";
    }


    @RequestMapping("/add")
    public String add1() {
        return "b/add";//打开创建图书界面
    }

    @PostMapping("/add")
    public String add(String id, String isbn, String name, String author, String publish, String price, String cat_id) {
        Book book = new Book();
        book.setId(Long.parseLong(id));
        book.setIsbn(isbn);
        book.setName(name);
        book.setAuthor(author);
        book.setPublish(publish);
        book.setPrice(Double.parseDouble(price));
        Category category = categoryService.getCategoryById(Long.parseLong(cat_id));
        book.setCategory(category);
        bookService.addbook(book);
        return "redirect:/book/list";
    }
}
